﻿using System;
using System.Net.Mail;
using BigfootMVC.Email;
using DotNetNuke.Common;
using DotNetNuke.Services.Mail;
using MailPriority = DotNetNuke.Services.Mail.MailPriority;

namespace BigfootMVC.DNN
{
    class DnnEmailProvider : IEmailProvider 
    {
        
        public void SendEmail(MailMessage msg)
        {
             SendEmail(msg, null);
        }

        public void SendEmail(MailMessage msg, params string[] attachments)
        {
            var priority = msg.Priority == System.Net.Mail.MailPriority.High
                                ? MailPriority.High
                                : (msg.Priority == System.Net.Mail.MailPriority.Low
                                        ? MailPriority.Low
                                        : MailPriority.Normal);
            var from = msg.From.Address;
            var to = msg.To.ToString();
            var cc = msg.CC.ToString();
            var bcc = msg.Bcc.ToString();
            var subject = msg.Subject;
            var bodyFormat = msg.IsBodyHtml ? MailFormat.Html : MailFormat.Text;
            var encoding = msg.BodyEncoding;
            var body = msg.Body;

            var SMTPServer = Convert.ToString(Globals.HostSettings["SMTPServer"]);
            var SMTPAuthentication = Convert.ToString(Globals.HostSettings["SMTPAuthentication"]);
            var SMTPUsername = Convert.ToString(Globals.HostSettings["SMTPUsername"]);
            var SMTPPassword = Convert.ToString(Globals.HostSettings["SMTPPassword"]);
            var SMTPEnableSSL = Globals.HostSettings["SMTPEnableSSL"].ToString().ToLowerInvariant() == "y";
            

            Mail.SendMail(from, to, cc, bcc, priority, subject, bodyFormat, encoding, body, attachments, 
                            SMTPServer, SMTPAuthentication, SMTPUsername, SMTPPassword, SMTPEnableSSL);
        }
    }
}
